<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class Products.
 *
 * @package namespace App\Repository\Models;
 */
class Products extends Model implements Transformable
{
    use TransformableTrait;
    use SoftDeletes;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['is_new', 'is_onsales', 'is_recommend', 'is_hot', 'first_cat', 'second_cat', 'third_cat', 'name', 'model','is_cupboard','succ_num','product_type',
        'brand_id', 'keywords', 'product_detail', 'place_origin', 'unit', 'delivery_time', 'guarantee_period', 'status', 'is_for_sale','after_sale_guarantee','custom_attrs',
        'area_id', 'sort', 'cover_image', 'images', 'is_recommend','product_desc','custom_cate','is_cupboard','shop_id','user_id','is_index_recommend','product_type'];

    protected $appends = ['available'];

    const STATUS_UNCHECKED = 0;
    const STATUS_CHECKED = 1;
    const STATUS_FAILED = 2;

    const IS_FOR_SALE_YES = 1;
    const IS_FOR_SALE_NO = 0;

    const IS_NORMAL_TYPE = 1; // 撮合商品
    const IS_QUALIFIED_TYPE = 2; // 找资质服务商品
    //返回商品状态是否为可用，必须同时满足审核通过及上架
    public function getAvailableAttribute()
    {
        if (isset($this->attributes['status'])) {
            if($this->attributes['status'] == self::STATUS_CHECKED && $this->attributes['is_for_sale'] == self::IS_FOR_SALE_YES){
                return true;
            }else{
                return false;
            }
        } else {
            return null;
        }
    }

    public function skus()
    {
        return $this->hasMany(ProductSkus::class, 'product_id', 'id');
    }
    
    public function attrs()
    {
        return $this->hasOne(ProductAttributes::class, 'product_id', 'id');
    }

    public function shop()
    {
        return $this->hasOne(Shop::class, 'sp_id', 'shop_id');
    }

    public function brand()
    {
        return $this->hasOne(ProductBrands::class, 'id', 'brand_id');
    }

    public function comments()
    {
        return $this->hasMany(OrderProductComment::class, 'product_id', 'id');
    }

    public function categories()
    {
      return $this->hasOne(ProductCategories::class, 'id', 'third_cat');
    }

    public function product_skus()
    {
        return $this->hasOne(ProductSkus::class, 'product_id', 'id');
    }



}
